﻿using System.Linq;
using Foothill.Common;
using Foothill.DataAccess.Interface;
using Foothill.Model;
using Foothill.Service.AdService;
using Foothill.Service.Caching;
using Foothill.Service.MiscInfo;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Foothill.Service.Test
{
    [TestClass]
    public class AdSearchServiceTest
    {
        private IAdSearchService _underTest;

        private ILocalCache _localCacheMock = new Mock<ILocalCache>().Object;
        private IAdSearchRepo _adSearchRepoMock = new Mock<IAdSearchRepo>().Object;
        private IAdListFilter _adListFilter = new Mock<IAdListFilter>().Object;
        private ISearchResultStatsCalculator _statsCalculator = new Mock<ISearchResultStatsCalculator>().Object;
        private IMiscInfoService _miscInfoService = new Mock<IMiscInfoService>().Object;

        [TestMethod]
        public void AssignMakeAndModelIds_ShouldSetIncludeAllMakesToTrue_WhenFirstMakeIdIsAllMakeId()
        {
            ForSaleAdAdvancedSearchParameter searchParam = new ForSaleAdAdvancedSearchParameter();
            searchParam.IncludeAllMakes = false;

            _underTest = new AdSearchService(_adSearchRepoMock, _localCacheMock, _adListFilter, _statsCalculator, _miscInfoService);

            _underTest.AssignMakeAndModelIds(searchParam, GlobalValues.AllMakeId, 1, 1, 1, 1, 1);

            Assert.IsTrue(searchParam.IncludeAllMakes);
        }

        [TestMethod]
        public void AssignMakeAndModelIds_ShouldNotAddNewItem_WhenFirstAndSecondAreTheSame()
        {
            ForSaleAdAdvancedSearchParameter searchParam = new ForSaleAdAdvancedSearchParameter();
            searchParam.IncludeAllMakes = false;

            _underTest = new AdSearchService(_adSearchRepoMock, _localCacheMock, _adListFilter, _statsCalculator, _miscInfoService);
            _underTest.AssignMakeAndModelIds(searchParam, 9, 8, 9, 8, 9, 8);

            Assert.IsTrue(searchParam.SelectedMakeAndModelIdLookup.Count == 1);

            var item = searchParam.SelectedMakeAndModelIdLookup.FirstOrDefault();
            Assert.AreEqual(item.MakeId, 9);
            Assert.AreEqual(item.ModelId, 8);
        }

        [TestMethod]
        public void AssignMakeAndModelIds_ShouldNotAddNewItem_WhenAllModelIsSelectedForTheMake()
        {
            ForSaleAdAdvancedSearchParameter searchParam = new ForSaleAdAdvancedSearchParameter();
            searchParam.IncludeAllMakes = false;

            _underTest = new AdSearchService(_adSearchRepoMock, _localCacheMock, _adListFilter, _statsCalculator, _miscInfoService);
            _underTest.AssignMakeAndModelIds(searchParam, 9, GlobalValues.AllModelId, 9, 7, 9, 8);

            Assert.IsTrue(searchParam.SelectedMakeAndModelIdLookup.Count == 1);

            var item = searchParam.SelectedMakeAndModelIdLookup.FirstOrDefault();
            Assert.AreEqual(item.MakeId, 9);
            Assert.AreEqual(item.ModelId, GlobalValues.AllModelId);
        }

        [TestMethod]
        public void AssignMakeAndModelIds_ShouldRemoveTheExistingItem_WhenNewModelIsAllModelForTheMake()
        {
            ForSaleAdAdvancedSearchParameter searchParam = new ForSaleAdAdvancedSearchParameter();
            searchParam.IncludeAllMakes = false;

            _underTest = new AdSearchService(_adSearchRepoMock, _localCacheMock, _adListFilter, _statsCalculator, _miscInfoService);
            _underTest.AssignMakeAndModelIds(searchParam, 9, 8, 9, GlobalValues.AllModelId, 4, 5);

            Assert.IsTrue(searchParam.SelectedMakeAndModelIdLookup.Count == 2);

            var item = searchParam.SelectedMakeAndModelIdLookup.FirstOrDefault(i=>i.MakeId == 9);
            Assert.AreEqual(item.MakeId, 9);
            Assert.AreEqual(item.ModelId, GlobalValues.AllModelId);
        }

        [TestMethod]
        public void AssignMakeAndModelIds_ShouldRemoveAllExistingItems_WhenTheThirdModelIsAllModelForTheMake()
        {
            ForSaleAdAdvancedSearchParameter searchParam = new ForSaleAdAdvancedSearchParameter();
            searchParam.IncludeAllMakes = false;

            _underTest = new AdSearchService(_adSearchRepoMock, _localCacheMock, _adListFilter, _statsCalculator, _miscInfoService);
            _underTest.AssignMakeAndModelIds(searchParam, 9, 8, 9, 6, 9 ,GlobalValues.AllModelId);

            Assert.IsTrue(searchParam.SelectedMakeAndModelIdLookup.Count == 1);

            var item = searchParam.SelectedMakeAndModelIdLookup.FirstOrDefault();
            Assert.AreEqual(item.MakeId, 9);
            Assert.AreEqual(item.ModelId, GlobalValues.AllModelId);
        }

        [TestMethod]
        public void AssignMakeAndModelIds_ShouldAddAllThreeItems_WhenThreeItemsAreForDifferentMake()
        {
            ForSaleAdAdvancedSearchParameter searchParam = new ForSaleAdAdvancedSearchParameter();
            searchParam.IncludeAllMakes = false;

            _underTest = new AdSearchService(_adSearchRepoMock, _localCacheMock, _adListFilter, _statsCalculator, _miscInfoService);
            _underTest.AssignMakeAndModelIds(searchParam, 9, 8, 10, 6, 11, GlobalValues.AllModelId);

            Assert.IsTrue(searchParam.SelectedMakeAndModelIdLookup.Count == 3);
        }
    }
}
